package com.iqiyi.pps.epg.web.webapp.action.stream;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.model.web.stream.TLiveStream;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.ApiHttpRequestUtil;
import com.iqiyi.pps.epg.web.utils.InnerApiUtil;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.context.annotation.Scope;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2014/12/25.
 */
@Namespace("/stream/liveStream")
@Scope("prototype")
public class LiveStreamAction extends BaseAction<TLiveStream> {

    private Page<TLiveStream> page;

    private String fatherId;
    private String streamName;
    private String uuid;
    private String isDef;
    private String streamIp;


    @Override
    @Action(value = "list")
    public String list() throws Exception {
        String url = String.format(InnerApiUtil.BASE_URL, InnerApiUtil.LIVE_STREAM_LIST);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fatherId", fatherId);
        if (StringUtils.isNotEmpty(streamName)) {
            params.put("streamName", streamName);
        }
        Map<String, Object> headers = new HashMap<String, Object>();
        headers.put("Sign", InnerApiUtil.TEST_SIGN);
        JSONObject jsonObject = ApiHttpRequestUtil.postMethod(url, headers, params);
        List<TLiveStream> tLiveStreams = new ArrayList<TLiveStream>();
        if (jsonObject != null && jsonObject.getString("code").equals(Constants.CODE_SUC)) {
            String data = jsonObject.getString("data");
            tLiveStreams = JSON.parseArray(data, TLiveStream.class);
        }

        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }
        page.setPageSize(10);
        page.setPageNo(1);
        page.setTotalCount(tLiveStreams.size());
        page.setResult(tLiveStreams);
        return SUCCESS;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    @Action(value = "save")
    public String save() throws Exception {
        return null;
    }

    @Override
    @Action(value = "delete")
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

    @Override
    public TLiveStream getModel() {
        return null;
    }

    public String getFatherId() {
        return fatherId;
    }

    public void setFatherId(String fatherId) {
        this.fatherId = fatherId;
    }

    public String getStreamName() {
        return streamName;
    }

    public void setStreamName(String streamName) {
        this.streamName = streamName;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getIsDef() {
        return isDef;
    }

    public void setIsDef(String isDef) {
        this.isDef = isDef;
    }

    public String getStreamIp() {
        return streamIp;
    }

    public void setStreamIp(String streamIp) {
        this.streamIp = streamIp;
    }

    public Page<TLiveStream> getPage() {
        return page;
    }

    public void setPage(Page<TLiveStream> page) {
        this.page = page;
    }
}
